package com.atguigu.gulimall.order.listener;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @title: OrderCloseListener
 * @Author syh
 * @Date: 2022/7/15 14:29
 * @Version 1.0
 */

@RabbitListener(queues = "order.release.order.queue")
@Service
public class OrderCloseListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
        try {
            System.out.println("收到消息,准备过期信息" + orderEntity.toString());
            // 关闭订单
            orderService.closeOrder(orderEntity);

            // 手动调用支付宝收单

            // 手动响应收到消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            // 关单失败,告诉MQ消息不要丢弃,继续保存,等到其他人来消费消息
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }

}
